package com.zcx.qshop.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;
import com.zcx.qshop.R;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.conn.JsonGetcodeAsyGet;
import com.zcx.qshop.conn.JsonRegistAsyGet;
import com.zcx.qshop.util.UtilRegular;

/**
 * Created by Administrator on 2015/8/9 0009.
 */
public class RegisterActivity extends QSActivity implements View.OnClickListener{

    @BoundView(R.id.register_back) private View back;

    @BoundView(R.id.register_username) private EditText username;

    @BoundView(R.id.register_password) private EditText password;

    @BoundView(R.id.register_password_delete) private ImageView password_delete;

    @BoundView(R.id.register_username_delete) private ImageView username_delete;

    @BoundView(R.id.register_verification) private EditText verification;

    @BoundView(R.id.register_get_verification) private TextView get_verification;

    @BoundView(R.id.register_register) private TextView register;

    private String vers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_register);

        back.setOnClickListener(this);

        get_verification.setOnClickListener(this);

        register.setOnClickListener(this);

        password_delete.setOnClickListener(this);

        username_delete.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.register_back:

                finish();

                break;

            case R.id.register_password_delete:

                password.setText("");

                break;

            case R.id.register_username_delete:

                username.setText("");

                break;

            case R.id.register_get_verification:

                String temp = username.getText().toString();

                if(temp.equals("")){

                    UtilToast.show(this, "请输入手机号");

                    return;

                }else if(!UtilRegular.mobile(temp)){

                    UtilToast.show(this, "手机格式错误");

                    return;

                }else{

                    new JsonGetcodeAsyGet(username.getText().toString(), "", new AsyCallBack<String>(){

                        @Override
                        public void onSuccess(String toast, int type, String s) throws Exception {

                            vers = s;

                            new CountDownTimer(60000, 1000) {

                                @Override
                                public void onTick(long millisUntilFinished) {

                                    get_verification.setText((millisUntilFinished / 1000) + "s");

                                }

                                @Override
                                public void onFinish() {

                                    get_verification.setText("获取验证码");

                                    get_verification.setClickable(true);

                                    username.setEnabled(true);

                                }

                            }.start();

                            get_verification.setClickable(false);

                            username.setEnabled(false);

                        }

                    }).execute(this);

                }

                break;

            case R.id.register_register:

                String urn = username.getText().toString(), psd = password.getText().toString(), ver = verification.getText().toString();

                if(urn.equals("")){

                    UtilToast.show(this, "请输入手机号");

                    return;

                }else if(!UtilRegular.mobile(urn)){

                    UtilToast.show(this, "手机格式错误");

                    return;

                }else if(psd.equals("")){

                    UtilToast.show(this, "请输入密码");

                    return;

                }else if(ver.equals("")){

                    UtilToast.show(this, "请输入验证码");

                    return;

                }else if(vers == null || !ver.equals(vers)){

                    UtilToast.show(this, "验证码错误");

                    return;

                }else{

                    new JsonRegistAsyGet(urn, psd, new AsyCallBack<String>(){

                        @Override
                        public void onSuccess(String toast, int type, String s) throws Exception {

                            QSApplication.QSPreferences.saveUid(s);

                            finish();

                        }

                        @Override
                        public void onEnd(String toast, int type) throws Exception {

                            UtilToast.show(RegisterActivity.this, toast);

                        }

                    }).execute(this);

                }

                break;

        }

    }
}
